<?php
namespace app\controllers;

use app\logic\ChannelLogic;
use Yii;


class ChannelController extends BackendController
{

	public function actionIndex(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $channelLogic = new ChannelLogic();
        $this->data['breadcrumbs'] = '渠道管理';
        //var_dump($orderLogic->getOrder($query, $page));exit;
        return $this->render('index', [
            'theme' => '/assets/',
            'channel' => $channelLogic->getChannel($query, $page),
            'query' => $query
        ]);
    }

    public function actionAdd(){
        if(!Yii::$app->request->post()){
            $this->data['breadcrumbs'] = ['渠道管理' => U('channel/index'), ' 添加渠道' => ''];
            return $this->render('add', [
                'actionUrl' => U('channel/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $channelLogic = new ChannelLogic();
            $logicRes = $channelLogic->addChannel($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增渠道成功');
                jump('channel/index');
            } else {
                Yii::$app->getSession()->setFlash('error','新增渠道失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }

    public function actionDelete(){
        $id = Yii::$app->request->get('channel_id');
        if($id){
            $channelLogic = new ChannelLogic();
            $res = $channelLogic->deleteById($id);
            if($res) YII::$app->session->setFlash('success','删除成功');
            else  YII::$app->session->setFlash('error','删除失败');
        }
        return $this->redirect(U('channel/index'));
    }

}